﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Core;

using System.Text.RegularExpressions;

namespace Ksamuelsen.Office.Outlook.Addin.VDialer {
    public partial class VDialerContactRibbon : OfficeRibbon {
        private ContactItem   _item;
        private ContactInfo   _contact;
        private bool          _event = false;


        public VDialerContactRibbon() {
            InitializeComponent();
            object o = this.Context;
            Close += new EventHandler(OnClose);
        }

        private void AddButton(ContactInfo.PhoneNumber number, int index) {
            RibbonButton btn = new RibbonButton();
            btn.Label = number.ToString();
            btn.Name = number.Id;
            btn.Tag = index;
            btn.Click += new System.EventHandler<Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs>(this.OnDialNumber);

            this.mnuCall.Items.Add(btn);

        }

        private void BuildMenu() {
            int idx = 0;
            
            _contact = new ContactInfo(_item);

            foreach (ContactInfo.PhoneNumber number in _contact.PhoneNumbers){
                AddButton(number, idx++);
            }

            if (mnuCall.Items.Count == 0)
                mnuCall.Enabled = false;


        }

        private void Vonage_Load(object sender, RibbonUIEventArgs e) {
            try {
                Inspector window = (Inspector)this.Context;
                
                _item = (ContactItem)window.CurrentItem;

                if (!_event){
                    _item.PropertyChange += new ItemEvents_10_PropertyChangeEventHandler(OnPropertyChanged);
                    _event = true;

                }

                BuildMenu();
            } catch (System.Exception ex) {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
        
        private void OnDialNumber(object sender, RibbonControlEventArgs e) {
            if (sender is RibbonButton) {
                RibbonButton btn = (RibbonButton)sender;
                _contact.SelectedIndex = (int)btn.Tag;
                Dialer dialer = new Dialer(_contact);
                dialer.Dial();
            }
        }

        private void OnPropertyChanged(string name) {
            if (String.IsNullOrEmpty(name) || (!ContactInfo.IsPropertySupported(name)))
                return;

            foreach (RibbonControl ctrl in mnuCall.Items){
                ctrl.Dispose();
            }
            mnuCall.Items.Clear();
            BuildMenu();

            
        }

        private void OnClose(object sender, EventArgs args) {
            if ((_item != null) && (_event))
                _item.PropertyChange -= new ItemEvents_10_PropertyChangeEventHandler(OnPropertyChanged);
        }

        

    }
}
